<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

/**
 * Fieldset contains and creates form fields and resides within a block
 *
 * @author Anthony Gallon
 * @package  jfx-core-classes
 */

require_once('Component.php');

/**
 * Fieldset contains and creates form fields and resides within a block
 *
 * @author Anthony Gallon
 * @package  jfx-core-classes
 */

class JFX_Form_Fieldset extends JFX_Form_Component
{
    /**
     * Stack containing all form field elements
     * @var array $components
     */
    protected $components = array();
    /**
     * Stack containing all classnames
     * @var array $classnames
     */
    protected $classnames = array();
    
    /**
     * Defines whether to show the fieldset border 
     * @var bool $showBorder
     */
    protected $showBorder;
    
    /**
     * Defines the alternative tag to use for legend if not showing border
     * @var string $altTag
     */
    protected $altTag;
    
    /**
     * Defines the HTML id name
     * @var string $id
     */
    protected $id;

    /**
     * Set some inital values
     * @param string $legend
     */
    public function __construct($legend){
        $this->legend = $legend;
        $this->showBorder(true);
    }

    /**
     * Add a long tag to the fieldset and get the new object
     * @param string $tagname
     * @param string $text
     * @return object JFX_Form_Components_LongTag
     */
    public function addLongTag($tagname, $text=''){
        $this->components[] = new JFX_Form_Components_LongTag($tagname, $text);
        $obj = $this->getLastComponent();
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        return $obj;
    }

    /**
     * Add a short tag and get the new object
     * @param string $tagname
     * @return object JFX_Form_Components_ShortTag
     */
    public function addShortTag($tagname){
        $this->components[] = new JFX_Form_Components_ShortTag($tagname);
        $obj = $this->getLastComponent();
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        return $obj;
    }

    /**
     * Add an input element to the form and get the new object
     * @param string $type
     * @param string $name
     * @param string $label
     * @param string $value
     * @param string $afterText
     * @return object ${'JFX_Form_Components_'.$type}
     */
    public function addInput($type, $name, $label, $value, $afterText=''){
        if($type == 'file') $this->formObj->setEncType('multipart/form-data');
        $this->components[] = new JFX_Form_Components_Input($type, $name, $label, $value, $afterText);
        $obj = $this->getLastComponent();
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        return $obj;
    }

    /**
     * Add a new drop menu and get the object
     * @param string $name
     * @param string $label
     * @param mixed $value
     * @param string $aftertext
     * @return object JFX_Form_Componets_Select
     */
    public function addSelect($name, $label, $value='', $aftertext=''){
        $this->components[] = new JFX_Form_Components_Select($name, $label, $value, $aftertext);
        $obj = $this->getLastComponent();
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        return $obj;
    }

    /**
     * Add a new date select field using drop menus
     * @param string $name
     * @param string $label
     * @param array $value
     * @param int $yearStart
     * @param int $yearEnd
     * @return object JFX_Form_Components_DateSelect
     */
    public function addDateSelect($name, $label, $value='', $yearStart='', $yearEnd=''){
        $this->components[] = new JFX_Form_Components_DateSelect($name, $label, $value, $yearStart, $yearEnd);
        $obj = $this->getLastComponent();
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        return $obj;
    }

    /**
     * Add a field to collect a NZ bank account number and get the new object
     * @param string $name
     * @param string $label
     * @param array $value
     * @return object JFX_Form_Components_BankNumber
     */
    public function addBankNumber($name, $label, $value=array()){
        $obj = new JFX_Form_Components_BankNumber($name, $label, $value);
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        $this->components[] = $obj;
        return $obj;
    }

    /**
     * Add a time select field using drop menus
     * @param string $name
     * @param string $label
     * @param array $value
     * @return object JFX_Form_Components_TimeSelect
     */
    public function addTimeSelect($name, $label, $value=''){
        $obj = new JFX_Form_Components_TimeSelect($name, $label, $value);
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        $this->components[] = $obj;
        return $obj;
    }

    /**
     * Add a time/date select field using drop menus
     * @param <type> $name
     * @param <type> $label
     * @param <type> $value
     * @return <type>
     */
    public function addTimeDateSelect($name, $label, $value=''){
        $obj = new JFX_Form_Components_TimeDateSelect($name, $label, $value);
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        $this->components[] = $obj;
        return $obj;
    }

    /**
     * Add a checkbox group to the fielset and get the new object
     * @param string $name
     * @return object JFX_Form_Components_CheckboxGroup
     */
    public function addCheckboxGroup($name){
        $this->components[] = new JFX_Form_Components_CheckboxGroup($name);
        $obj = $this->getLastComponent();
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        return $obj;
    }

    /**
     * Add a radio group to the fieldset and get the new object
     * @param string $name
     * @param string $value
     * @return object JFX_Form_Components_RadioGroup
     */
    public function addRadioGroup($name, $label='', $value=''){
        $this->components[] = new JFX_Form_Components_RadioGroup($name, $label, $value);
        $obj = $this->getLastComponent();
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        return $obj;
    }

    /**
     * Get the object that was laste added to the element stack
     * @return object JFX_Form_Component
     */
    public function getLastComponent(){
        return $this->components[(count($this->components)-1)];
    }

    /**
     * Find out how many elements there are in this fieldset
     * @return int $count
     */
    public function countComponents(){
        return count($this->components);
    }

    /**
     * Set whether to show the border or not
     * @param bool $bool
     * @param string $altTag
     */
    public function showBorder($bool, $altTag='p'){
        $this->showBorder = (bool) $bool;
        $this->altTag = $altTag;
    }

    /**
     * Get the fieldset and all elements as an HTML string
     * @return string $html
     */
    public function render(){
        $showBorder = ($this->showBorder) ? '':  ' no-border ' ;
        $legend = '<legend>'.$this->legend.'</legend>';
        $returnString = "\n".'      <fieldset class="'.$showBorder.' '.implode(' ', $this->classnames).'" id="'.$this->getId().'">';
        $returnString .= "\n".'        '.$legend;
        foreach($this->components as $component){
            $returnString .= "\n          ".$component->render();
        };
        $returnString .= "\n".'      </fieldset>';
        return $returnString;
    }

}
